package cn.hznc.controller.toolfork;

import cn.hznc.aspect.BusinessType;
import cn.hznc.aspect.Log;
import cn.hznc.domain.request.toolfork.*;
import cn.hznc.domain.response.PageResult;
import cn.hznc.domain.response.ResponseBody;
import cn.hznc.exception.ToolManageException;
import cn.hznc.controller.service.toolfork.AssemblyToolService;
import cn.hznc.permission.Permission;
import cn.hznc.vo.AssemblyToolDetailVo;
import cn.hznc.vo.AssemblyToolVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * 刀具总成接口
 *
 * @author xiahuanyu
 * @date 2023/3/3
 */
@RestController
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("/assemblyTool")
public class AssemblyToolController {

    @Resource
    public AssemblyToolService assemblyToolService;

    /**
     * 创建总成刀具
     *
     * @param createAssemblyToolReq createAssemblyToolReq
     * @return Boolean
     */
    @RequestMapping(value = "/createAssemblyTool", method = RequestMethod.POST)
    @Log(title = "新增总成" , businessType = BusinessType.INSERT)
    public ResponseBody createAssemblyTool(@RequestBody CreateAssemblyToolReq createAssemblyToolReq) {
        return ResponseBody.success(assemblyToolService.createAssemblyTool(createAssemblyToolReq));
    }

    /**
     * 修改现有总成刀具数据格式
     *
     * @return Boolean
     */
    @RequestMapping(value = "/changeAssemblyTool", method = RequestMethod.GET)
    public ResponseBody changeAssemblyTool() {
        return ResponseBody.success(assemblyToolService.changeAssemblyTool());
    }


    /**
     * 编辑总成刀具
     *
     * @param editAssemblyToolReq editAssemblyToolReq
     * @return Boolean
     */
    @RequestMapping(value = "/editAssemblyTool", method = RequestMethod.POST)
    @Log(title = "编辑总成刀具" , businessType = BusinessType.UPDATE)
    @Permission(title = "编辑总成刀具" , permissions = {"assemblyTool.update"})
    public ResponseBody<Boolean> editAssemblyTool(@RequestBody EditAssemblyToolReq editAssemblyToolReq) {
        try {
            assemblyToolService.editAssemblyTool(editAssemblyToolReq);
        } catch (ToolManageException te) {
            return ResponseBody.error(te.getCode(), te.getMsg());
        }
        return ResponseBody.success(Boolean.TRUE);
    }

    /**
     * 删除总成刀具
     *
     * @param ids ids
     * @return Boolean
     */
    @RequestMapping(value = "/deleteAssemblyTool", method = RequestMethod.POST)
    @Log(title = "删除总成" , businessType = BusinessType.DELETE)
    @Permission(title = "删除总成" , permissions = {"assemblyTool.delete"})
    public ResponseBody<Boolean> deleteAssemblyTool(@RequestBody List<Integer> ids) {
        try {
            assemblyToolService.deleteAssemblyTool(ids);
        } catch (ToolManageException te) {
            return ResponseBody.error(te.getCode(), te.getMsg());
        }
        return ResponseBody.success(Boolean.TRUE);
    }

    /**
     * 总成上传图片
     *
     * @param
     * @return Boolean
     */
    @RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
    public ResponseBody uploadFile(@RequestPart(value = "threeDimensionalModel", required = false) MultipartFile threeDimensionalModel,
                                   @RequestPart(value = "threeDimensionalModelStl", required = false) MultipartFile threeDimensionalModelStl,
                                   @RequestPart(value = "name" , required = false) String name,
                                   @RequestPart(value = "flag") String flag) {
        try {
            return ResponseBody.success(assemblyToolService.uploadFile(threeDimensionalModel, threeDimensionalModelStl, name , flag));
        } catch (ToolManageException e) {
            return ResponseBody.error(e.getCode(), e.getMessage());
        }
    }

    /**
     * 查询总成刀具
     *
     * @param
     * @return Boolean
     */
    @RequestMapping(value = "/queryAssemblyTool", method = RequestMethod.POST)
    @Permission(title = "查询总成刀具" , permissions = {"assemblyTool.read"})
    public ResponseBody<PageResult<AssemblyToolVo>> queryAssemblyTool(@RequestBody QueryAssemblyToolReq queryAssemblyToolReq) {
        return ResponseBody.success(assemblyToolService.queryAssemblyTool(queryAssemblyToolReq));
    }

    /**
     * 查询总成刀具详情
     *
     * @param
     * @return Boolean
     */
    @RequestMapping(value = "/queryAssemblyToolDetail", method = RequestMethod.POST)
    public ResponseBody<AssemblyToolDetailVo> queryAssemblyToolDetail(@RequestBody QueryToolDetail queryToolDetail) {
        try {
            return ResponseBody.success(assemblyToolService.queryAssemblyToolDetail(queryToolDetail));
        } catch (ToolManageException te) {
            return ResponseBody.error(te.getCode(), te.getMsg());
        }
    }

//    @RequestMapping(value = "/matchByCode", method = RequestMethod.POST)
//    public ResponseBody<AssemblyToolDetailVo> matchByCode(@RequestBody QueryToolDetail queryToolDetail) {
//        try {
//            return ResponseBody.success(assemblyToolService.queryAssemblyToolDetail(queryToolDetail));
//        } catch (ToolManageException te) {
//            return ResponseBody.error(te.getCode(),te.getMsg());
//        }
//    }

    @RequestMapping(value = "/editAssemblyToolCuttingParams", method = RequestMethod.POST)
    @Log(title = "编辑总成切削参数" , businessType = BusinessType.UPDATE)
    @Permission(title = "编辑总成参数" , permissions = {"assemblyTool.updateParam"})
    public ResponseBody editAssemblyToolCuttingParams(@RequestBody EditAssemblyToolParamsReq editAssemblyToolParamsReq) {
        try {
            return ResponseBody.success(assemblyToolService.editAssemblyToolCuttingParams(editAssemblyToolParamsReq));
        } catch (ToolManageException te) {
            return ResponseBody.error(te.getCode(), te.getMsg());
        }
    }

    @RequestMapping(value = "/editAssemblyToolParams", method = RequestMethod.POST)
    public ResponseBody editAssemblyToolParams(@RequestBody EditAssemblyToolParamReloadReq editAssemblyToolParamsReq) {
        try {
            return ResponseBody.success(assemblyToolService.editAssemblyToolParams(editAssemblyToolParamsReq));
        } catch (ToolManageException te) {
            return ResponseBody.error(te.getCode(), te.getMsg());
        }
    }


}
